package cn.edu.scau.cmi.chenyipeng.Adapter;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class AOO_Controller {

	private UnsortedStudents all = new UnsortedStudents();
	
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableColumn<Student, Integer> IDCol;

    @FXML
    private TextField nameTF;

    @FXML
    private TableView<Student> markTV;

    @FXML
    private TextField idTF;

    @FXML
    private Button sortB;

    @FXML
    private TableColumn<Student, String> NameCol;

    @FXML
    void AddThenSort(ActionEvent event) {
    	int tempID = Integer.valueOf(idTF.getText());
    	String tempName = nameTF.getText();
    	Student stu = new Student(tempID,tempName);
    	all.addStudent(stu);
    	AdapterOfClass aoc = new AdapterOfClass();
    	//System.out.println(all.getALL().size());
    	showData(aoc.sort(all));
    }

    @FXML
    void showData(ArrayList<Student> sortedStudents) {
    	ObservableList<Student> list = FXCollections.observableArrayList();
    	IDCol.setCellValueFactory(new PropertyValueFactory<Student,Integer>("id"));
    	NameCol.setCellValueFactory(new PropertyValueFactory<Student,String>("name"));;
    	int i;
    	for(i = 0; i < sortedStudents.size(); ++i) {
    		list.add(sortedStudents.get(i));
    	}
    	markTV.setItems(list);
    }
    @FXML
    void initialize() {
        assert IDCol != null : "fx:id=\"IDCol\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";
        assert nameTF != null : "fx:id=\"nameTF\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";
        assert markTV != null : "fx:id=\"markTV\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";
        assert idTF != null : "fx:id=\"idTF\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";
        assert sortB != null : "fx:id=\"sortB\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";
        assert NameCol != null : "fx:id=\"NameCol\" was not injected: check your FXML file 'AdapterOfObjectUI.fxml'.";

    }
}
